package top.likeqc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import top.likeqc.common.lang.Const;
import top.likeqc.entity.SysRole;
import top.likeqc.entity.SysUser;
import top.likeqc.entity.SysUserRole;
import top.likeqc.entity.Teacher;
import top.likeqc.mapper.TeacherMapper;
import top.likeqc.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.likeqc.service.SysRoleService;
import top.likeqc.service.SysUserRoleService;
import top.likeqc.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author likeqc
 * @since 2022-04-09
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Override
    public Teacher edit(Teacher teacher) {

        SysUser sysUser1 = sysUserService.getByUsername(teacher.getCode());
        Teacher teacher1 = getOne(new QueryWrapper<Teacher>().eq("code", teacher.getCode()));

        if (teacher.getId() == null && sysUser1 == null && teacher1 == null) {
            // 新建教师的同时创建用户
            SysUser sysUser = new SysUser();
            sysUser.setUsername(teacher.getCode());
            // 默认密码，头像
            sysUser.setPassword(passwordEncoder.encode(Const.DEFULT_PASSWORD));
            sysUser.setAvatar(Const.DEFULT_AVATAR);
            sysUserService.save(sysUser);
            teacher.setUserId(sysUser.getId());
            save(teacher);
            // 用户的角色是教师
            SysRole teacherRole = sysRoleService.getOne(new QueryWrapper<SysRole>().eq("code", "teacher"));
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getId());
            sysUserRole.setRoleId(teacherRole.getId());
            sysUserRoleService.save(sysUserRole);
        } else {
            updateById(teacher);
        }

        return teacher;
    }

    @Override
    public void save(List<Teacher> list) {
        // saveBatch(list);
        list.forEach(teacher -> edit(teacher));
    }

    @Override
    @Transactional
    public void delete(Long[] userIds) {
        List<Teacher> teacherList = list(new QueryWrapper<Teacher>().in("id", userIds));
        List<Long> userIds_ = new ArrayList();
        teacherList.forEach(teacher -> userIds_.add(teacher.getUserId()));
        // 删除教师表信息
        remove(new QueryWrapper<Teacher>().in("id", userIds));
        // 删除用户表信息
        sysUserService.remove(new QueryWrapper<SysUser>().in("id", userIds_));
        // 删除用户角色表信息
        sysUserRoleService.remove(new QueryWrapper<SysUserRole>().in("user_id", userIds_));
    }
}
